package com.base.cn.platform.os.controller.user.login;

import com.base.cn.platform.os.common.j2cache.CustomCacheChannel;
import com.base.cn.platform.os.common.rsa.WebRsaUtils;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.common.utils.user.ProfileType;
import com.base.cn.platform.os.common.utils.user.UserExpandFrom;
import com.base.cn.platform.os.controller.cmmon.ApiBaseController;
import com.base.cn.platform.os.service.manage.user.login.UserLoginService;
import com.base.cn.platform.os.service.manage.user.user.CuserProfileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 第三方登录接口
 *
 * @author s.li
 * @create 2018-09-05-11:22
 */
@RestController
@RequestMapping("/api/user")
public class ApiThirdLoginController extends ApiBaseController {

    @Autowired
    private CuserProfileService cuserProfileService;
    @Autowired
    private UserLoginService userLoginService;
    @Autowired
    private CustomCacheChannel customCacheChannel;


    /**
     * 设置登录密码
     * @param userId 用户ID
     * @param userPassword 用户密码
     * @param confirmPassword 用户砍确认密码
     * @return Map<String,Object>
     */
    @RequestMapping("/setPerfectPwd")
    @ResponseBody
    public Map<String,Object> setPerfectPwd(@RequestParam("userId") BigDecimal userId,
                                            @RequestParam("userPassword") String userPassword,
                                            @RequestParam("confirmPassword") String confirmPassword,
                                            @RequestParam("rsaSign") String rsaSign) throws Exception{
        Map<String,Object> resultMap = userLoginService.setPerfectPwd(userId,userPassword,confirmPassword,rsaSign);
        if(!ResultUtil.getSuccess(resultMap)){
            Map<String,String> keyMap = WebRsaUtils.getRSAData(customCacheChannel);
            resultMap.put("keyMap",keyMap);
        }
        return resultMap;
    }

    /**
     * 第三方登录接口
     * @param appType 登录方式，QQ，微信
     * @param appId
     * @param cusName 用户名
     * @param avatar 头像图片路径
     * @return Map<String,Object>
     */
    @RequestMapping("/bindingApp")
    public Map<String,Object> bindingApp(HttpServletRequest request,
                                         @RequestParam(value="appType") String appType,
                                         @RequestParam(value="appId") String appId,
                                         @RequestParam(value="cusName",required = false) String cusName,
                                         @RequestParam(value="openId",required = false,defaultValue = "") String openId,
                                         @RequestParam(value="avatar",required=false,defaultValue = "") String avatar,
                                         @RequestParam(value = "from",required = false,defaultValue = "0") int from){


        Map<String,Object>  profile = new HashMap<>();
        profile.put("profileType", appType);
        profile.put("value",appId);
        profile.put("avatar", avatar);
        profile.put("openId", openId);
        profile.put("version", request.getHeader("version"));
        System.out.println(request.getHeader("version"));
        if (ObjectUtils.isNotEmpty(cusName)) {
            profile.put("name", cusName);
        }else{
            profile.put("name", "ios_"+ StringUtils.getRandStr(6));
        }
        String regType = null;
        if(appType.equals(ProfileType.QQ.toString())){
            if(from==1){
                regType = UserExpandFrom.androidThirdQQ.getKey();
            }else if(from ==2){
                regType = UserExpandFrom.iosThirdQQ.getKey();
            }
        }else if(appType.equals(ProfileType.WEIXIN.toString())){
            if(from==1){
                regType = UserExpandFrom.androidThirdWeiXin.getKey();
            }else if(from ==2){
                regType = UserExpandFrom.iosThirdWeiXin.getKey();
            }
        }else if (appType.equals(ProfileType.IOSLOGIN.name())) {
            regType = UserExpandFrom.iosLogin.getKey();
        }
        if(regType==null){
            return ResultUtil.ERROR("数据错误");
        }
        Map<String,Object> resultMap = cuserProfileService.saveUserProfile(profile, regType);
        return resultMap;
    }

}
